import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pplive/models/resp_model.dart';
import 'package:pplive/models/user_card.dart';
import 'package:pplive/routes/AppRoute.dart';

import '../../common/api_service.dart';

class MineItem {
  IconData? icon;
  String title;
  String subTitle;

  Function(MineItem item)? onTap;

  MineItem(
      {this.icon, required this.title, required this.subTitle, this.onTap});

  void click() {
    onTap?.call(this);
  }
}

class MinePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  List<MineItem> items = [];
late ThemeData theme;
  @override
  void initState() {
    super.initState();

    setState(() {

      items.add(MineItem(
          icon: Icons.severe_cold,
          title: '联系客服',
          subTitle: '',
          onTap: (item) {
            print("点击了联系客服");
          }));

      items.add(MineItem(
          icon: Icons.view_array,
          title: '当前版本',
          subTitle: 'V 1.0',
          onTap: (item) {
            print("点击了当前版本");
          }));

      items.add(MineItem(
          icon: Icons.account_circle,
          title: '关于我们',
          subTitle: '',
          onTap: (item) {
            print("点击了关于我们");
          }));
    });
  }

  Future<RespModel<UserCard>> fetchData() async {
    ApiService apiService = ApiService();
    try {
      return await apiService
          .post<UserCard>("/pu/get", params: {"tid": "90079454"});
    } catch (e) {
      return RespModel<UserCard>(code: "-1", data: null, message: '$e');
    }
  }

  @override
  Widget build(BuildContext context) {
    theme = Theme.of(context);
    var themeColor = Color(0xffC600CD);
    return Scaffold(
      backgroundColor: theme.colorScheme.onSurface,
      body: Stack(
        children: [
          Positioned(
              top: 0,
              left: 0,
              right: 0,
              height: 390,
              child: Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image:
                            AssetImage("assets/images/pp_page_topBg@2x.png"))),
              )),
          SafeArea(
              child: Container(
            child: FutureBuilder(
                future: fetchData(),
                builder: (context, snapshot) {
                  if (snapshot.hasData && snapshot.data!.data != null) {
                    var data = snapshot.data!.data!;
                    var user = data.user;

                    List<MineItem> tabs = [];

                    tabs.add(MineItem(
                        title: '好友',
                        subTitle: '${data.friendCount}',
                        onTap: (item) {
                          print("点击了好友");
                          AppPage.mineSub.go(item,  {"api": ApiService(isPost: true, path: "/pr/friend/list", params: {"limit": 100, "offset": 0}).toJSONString()});
                        }));
                    tabs.add(MineItem(
                        title: '粉丝',
                        subTitle: '${data.fansCount}',
                        onTap: (item) {
                          print("点击了粉丝");
                          var api = ApiService(isPost: true, path: "/pr/list/fans", params: {"limit": 100, "offset": 0}).toJSONString();
                          AppPage.mineSub.go(item, {"api": api});
                        }));
                    tabs.add(MineItem(
                        title: '关注',
                        subTitle: '${data.followCount}',
                        onTap: (item) {
                          print("点击了关注");
                          var api = ApiService(isPost: true, path: "/pr/list/follow", params: {"limit": 100, "offset": 0}).toJSONString();
                          AppPage.mineSub.go(item, {"api": api});
                        }));
                    tabs.add(MineItem(
                        title: '访客',
                        subTitle: '${data.visitCount}',
                        onTap: (item) {
                          print("点击了访客");
                          AppPage.mineSub.go(item,  {"api": ApiService(isPost: true, path: "/pu/visit/record/list", params: {"limit": 100, "offset": 0}).toJSONString()});
                        }));

                    return CustomScrollView(
                      slivers: [
                        SliverToBoxAdapter(
                            child: Padding(
                          padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Stack(
                                alignment: Alignment.center,
                                children: [
                                  Container(
                                    height: 100,
                                    child: Center(
                                      child: CircleAvatar(
                                        backgroundImage:
                                            CachedNetworkImageProvider(
                                                user?.head ?? ""),
                                        radius: 30,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              SizedBox(width: 6),
                              Expanded(
                                  child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    user?.name ?? "",
                                    style: const TextStyle(
                                        color: Colors.white, fontSize: 20),
                                    maxLines: 1, // 限制最大行数
                                    overflow:
                                        TextOverflow.ellipsis, // 超出部分用省略号表示
                                  ),
                                  Text('ID: ${user?.uid ?? ""}'),
                                ],
                              ))
                            ],
                          ),
                        )),
                        SliverToBoxAdapter(
                            child: Padding(
                          padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(12),
                              border: Border.all(
                                color: themeColor,
                                width: 0.5,
                              ),
                              gradient: LinearGradient(
                                begin: Alignment.topLeft,
                                end: Alignment.bottomRight,
                                colors: [
                                  const Color(0x00bb47ff).withOpacity(0.27),
                                  // #bb47ff00 45.68%
                                  const Color(0x1abb47ff).withOpacity(0.1),
                                  // #bb47ff1a 100%
                                  const Color(0x004763ff).withOpacity(0.31),
                                  // #4763ff00 51.26%
                                  const Color(0x1a4763ff).withOpacity(0.1),
                                  // #4763ff1a 101.69%
                                  const Color(0x1a6d6dff).withOpacity(0.1),
                                  // #6d6dff1a 1.55%
                                  const Color(0x1aff8eff).withOpacity(0.1),
                                  // #ff8eff1a 98.45%
                                ],
                                stops: const [
                                  0.4568,
                                  1.0,
                                  0.5126,
                                  1.0169,
                                  0.0155,
                                  0.9845
                                ],
                              ),
                            ),
                            padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: List.generate(tabs.length, (index) {
                                return InkWell(
                                    onTap: () {
                                      tabs[index].click();
                                    },
                                    child: Column(
                                      children: <Widget>[
                                        Text('${tabs[index].subTitle}',
                                            style: const TextStyle(
                                                fontFamily: "PingFang SC",
                                                fontSize: 18,
                                                color: Colors.white)),
                                        const SizedBox(height: 5),
                                        Text(tabs[index].title,
                                            style: TextStyle(
                                                fontFamily: "PingFang SC",
                                                fontSize: 12,
                                                fontWeight: FontWeight.w400,
                                                color: Colors.white
                                                    .withOpacity(0.4))),
                                      ],
                                    ));
                              }),
                            ),
                          ),
                        )),
                        SliverToBoxAdapter(child: Container(height: 24)),
                        SliverToBoxAdapter(
                            child: ClipRRect(
                                borderRadius: BorderRadius.circular(16),
                                child: Padding(
                                    padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                                    child: Container(
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(12),
                                          border: Border.all(
                                            color: themeColor,
                                            width: 0.5,
                                          ),
                                          gradient: LinearGradient(
                                            begin: Alignment.topLeft,
                                            end: const Alignment(0.12, -0.78),
                                            colors: [
                                              const Color(0x00bb47ff)
                                                  .withOpacity(0.27),
                                              // #bb47ff00 45.68%
                                              const Color(0x1abb47ff)
                                                  .withOpacity(0.1),
                                              // #bb47ff1a 100%
                                              const Color(0x004763ff)
                                                  .withOpacity(0.31),
                                              // #4763ff00 51.26%
                                              const Color(0x1a4763ff)
                                                  .withOpacity(0.1),
                                              // #4763ff1a 101.69%
                                              const Color(0x1a6d6dff)
                                                  .withOpacity(0.1),
                                              // #6d6dff1a 1.55%
                                              const Color(0x1aff8eff)
                                                  .withOpacity(0.1),
                                              // #ff8eff1a 98.45%
                                            ],
                                            stops: const [
                                              0.4568,
                                              1.0,
                                              0.5126,
                                              1.0169,
                                              0.0155,
                                              0.9845
                                            ],
                                          ),
                                        ),
                                        padding: EdgeInsets.all(10),
                                        child: SingleChildScrollView(
                                            child: Column(
                                          children: List.generate(
                                              items.length,
                                              (index) => Column(children: [
                                                    Container(
                                                      child: ListTile(
                                                        leading: Icon(
                                                            items[index].icon,
                                                            color: themeColor),
                                                        title: Text(
                                                          items[index].title,
                                                          style: const TextStyle(
                                                              fontSize: 12,
                                                              color: Color(
                                                                  0xffFEDAFF)),
                                                        ),
                                                        trailing: Text(
                                                          items[index].subTitle,
                                                          style: const TextStyle(
                                                              fontSize: 11,
                                                              color: Color(
                                                                  0xffFEDAFF)),
                                                        ),
                                                        onTap: () {
                                                          // 处理点击事件，例如导航到'设置'页面
                                                          items[index].click();
                                                        },
                                                      ),
                                                    ),
                                                    if (index <
                                                        items.length - 1)
                                                      const Divider(
                                                        height: 6,
                                                        color:
                                                            Color(0x14FFFFFF),
                                                      )
                                                  ])),
                                        )))))),
                        SliverToBoxAdapter(child: Container(height: 24)),
                        SliverToBoxAdapter(
                            child: Padding(
                                padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(8),
                                  ),
                                  child: ListTile(
                                    title: const Text(
                                      '退出',
                                      style: TextStyle(
                                        fontFamily: "PingFang SC",
                                        fontSize: 12,
                                        fontWeight: FontWeight.w400,
                                      ),
                                      textAlign:
                                          TextAlign.center, // 对于单行文本，这通常是可选的
                                    ),
                                    onTap: () {},
                                  ),
                                ))),
                      ],
                    );
                  }
                  return Center(child: CircularProgressIndicator());
                }),
          ))
        ],
      ),
    );
  }
}
